package org.example.demo3.server.service.impl;

import org.example.demo3.common.exception.*;
import org.example.demo3.pojo.dto.AdminLoginDTO;
import org.example.demo3.pojo.dto.AdminRegisterDTO;
import org.example.demo3.pojo.dto.AdminUpdateDTO;
import org.example.demo3.pojo.entity.Admin;
import org.example.demo3.pojo.vo.AdminVO;
import org.example.demo3.server.mapper.AdminMapper;
import org.example.demo3.server.service.AdminService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class AdminServiceImpl implements AdminService{
    @Autowired
    private AdminMapper adminMapper;


    private static final String EMAIL_REGEX =
        "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@" +
        "(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";

    private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX);

     private static final String PASSWORD_REGEX =
        "^(?=.*[0-9])(?=.*[A-Z]).{8,}$";

    private static final Pattern PASSWORD_PATTERN = Pattern.compile(PASSWORD_REGEX);

    /**
     * 登录接口
     * @param adminLoginDTO
     * @return
     */
    public Admin adminLogin(AdminLoginDTO adminLoginDTO){
        String username= adminLoginDTO.getUsername();
        String password=adminLoginDTO.getPassword();

        Admin admin=adminMapper.queryAdminByUsername(username); //根据username查询admin

        if(admin==null){
            throw new AccountNotFoundException("账号no found!");
        }

        if(!admin.getPassword().equals(password)){
            throw new LoginFailedException("密码 wrong!");
        }

        return admin;
    }

    /**
     * 注册接口
     * @param adminRegisterDTO
     */
    public void adminRegister(AdminRegisterDTO adminRegisterDTO){
        String name=adminRegisterDTO.getName();
        if(name.length()<3){
            throw new NameShortException("用户名不应少于三个字符哦");
        }

        Admin admin2=adminMapper.queryAdminByname(name);
        if(admin2!=null && admin2.getName().equals(name)){
            throw new NameSameException("用户名已被使用！");
        }

        String username=adminRegisterDTO.getUsername();
        Matcher matcher = EMAIL_PATTERN.matcher(username);
        if(!matcher.matches()){
            throw new EmailWrongException("邮箱格式错误！");
        }
        Admin admin3=adminMapper.queryAdminByUsername(username);
        if(admin3!=null && admin3.getUsername().equals(username)){
            throw new EmailSameException("该邮箱已注册过账号！");
        }

        String password=adminRegisterDTO.getPassword();

        Matcher matcher2 = PASSWORD_PATTERN.matcher(password);
        if(!matcher2.matches()){
            throw new PasswordWeakException("密码强度不够！");
        }

        Admin admin=new Admin();
        BeanUtils.copyProperties(adminRegisterDTO,admin);
        admin.setCreateTime(LocalDateTime.now());
        admin.setUpdateTime(LocalDateTime.now());
        admin.setImg("https://fileupload191.oss-cn-beijing.aliyuncs.com/11.jpg");
        admin.setStatus(1);   //1为启用，0为未启用
        adminMapper.insertAdmin(admin);

        return;
    }

    /**
     * 用户信息查询
     * @param name
     */
    public AdminVO adminQuery(String name){
        return adminMapper.queryAdminVOByname(name);
    }

    /**
     * 更新头像
     * @param adminUpdateDTO
     */
    public void imgUpdate(AdminUpdateDTO adminUpdateDTO){
        adminMapper.updateImgByName(adminUpdateDTO.getName(),adminUpdateDTO.getImg());
        return;
    }
}
